package actions;

import factoryGame.ObjectFactory;
import game.constants.ReferenceAction;
import game.constants.ReferenceObject;
import game.constants.ReferenceSpecialFeature;
import interfaces.ActionInterface;
import interfaces.impl.Action;

import java.util.ArrayList;

import objects.SpetialFeature;
import objects.WorldObject;
import universe.Universe;
import constants.Constants;



public class ActionCreateStarcraftObject extends Action{
	
	private int objectReference;
	private double price=0;
	private double vespeneGas=0;
	

	@Override
	public ActionInterface cloneAction() {
		ActionCreateStarcraftObject action = new ActionCreateStarcraftObject();
		action.modifyAction(this);
		return action;
	}

	@Override
	public boolean processActionChild(Universe universe) {
		
		if(haveEnoughtMinerals(universe)==false){
			return false;
		}
		if(haveEnoughtVespeneGas(universe)==false){
			return false;
		}
		
		removeMinerals(universe);
		removeVespeneGas(universe);
		
		
		
		WorldObject objectToCreate=ObjectFactory.createWorldObject(objectReference, target.getOwner(), target.getPosition().getX()+target.getImageObject().getImageToShow().getWidth(), target.getPosition().getY(),Constants.ASSIGN_SPECIAL_FEATURE);
		objectToCreate.setActive(false);
		objectToCreate.addSpetialFeature(new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_IS_BUILDING,"is Building",Constants.TRUE));
		
		SpetialFeature currentLife= objectToCreate.getFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_CURRENT_LIFE);
		if(currentLife==null){
			return false;
		}
		currentLife.setValue(1);
		objectToCreate.setOwner(target.getOwner());
		target.addObject(objectToCreate);
		
		return true;
	}

	private void removeVespeneGas(Universe universe) {
		
		ArrayList<WorldObject> listCommandCenter = universe.getListObjects(ReferenceObject.OBJECT_REFERENCE_COMMAND_CENTER1);
		
		for(WorldObject obj : listCommandCenter){
			if(obj.getOwner()!=target.getOwner()){
				continue;
			}
			SpetialFeature mineralQuantity = obj.getFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_VESPENE_GAS_QUANTITY);
			if(mineralQuantity==null){continue;}
			double mineralsToSubstract = mineralQuantity.getValue()-vespeneGas;
			if(mineralsToSubstract<0){
				mineralQuantity.setValue(0);
				vespeneGas=mineralsToSubstract*-1;
				continue;
			}
			mineralQuantity.setValue(mineralQuantity.getValue()-vespeneGas);
			vespeneGas=0;
			break;
		}
		
	}

	private void removeMinerals(Universe universe) {
		ArrayList<WorldObject> listCommandCenter = universe.getListObjects(ReferenceObject.OBJECT_REFERENCE_COMMAND_CENTER1);
		
		for(WorldObject obj : listCommandCenter){
			if(obj.getOwner()!=target.getOwner()){
				continue;
			}
			SpetialFeature mineralQuantity = obj.getFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_MINERAL_QUANTITY);
			if(mineralQuantity==null){continue;}
			double mineralsToSubstract = mineralQuantity.getValue()-price;
			if(mineralsToSubstract<0){
				mineralQuantity.setValue(0);
				price=mineralsToSubstract*-1;
				continue;
			}
			mineralQuantity.setValue(mineralQuantity.getValue()-price);
			price=0;
			break;
		}
		
	}

	private boolean haveEnoughtVespeneGas(Universe universe) {
		if(vespeneGas==0){
			return true;
		}
		ArrayList<WorldObject> listCommandCenter = universe.getListObjects(ReferenceObject.OBJECT_REFERENCE_COMMAND_CENTER1);
		double vespeneGas=0;
		for(WorldObject obj : listCommandCenter){
			if(obj.getOwner()!=target.getOwner()){
				continue;
			}
			SpetialFeature vespeneGasQuantity = obj.getFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_VESPENE_GAS_QUANTITY);
			if(vespeneGasQuantity==null){continue;}
			vespeneGas=vespeneGas+vespeneGasQuantity.getValue();
		}
		
		if(vespeneGas<this.vespeneGas){
			universe.generateDialogMessage(target.getOwner(),"NOT ENOUGHT VESPENE GAS");
			return false;
		}
		System.out.println("GAS: "+vespeneGas+" VespeneFas: "+vespeneGas );
		return true;
	}

	private boolean haveEnoughtMinerals(Universe universe) {
		if(price==0){
			return true;
		}
		ArrayList<WorldObject> listCommandCenter = universe.getListObjects(ReferenceObject.OBJECT_REFERENCE_COMMAND_CENTER1);
		double minerals=0;
		for(WorldObject obj : listCommandCenter){
			if(obj.getOwner()!=target.getOwner()){
				continue;
			}
			SpetialFeature mineralQuantity = obj.getFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_MINERAL_QUANTITY);
			if(mineralQuantity==null){continue;}
			minerals=minerals+mineralQuantity.getValue();
		}
		
		if(minerals<price){
			universe.generateDialogMessage(target.getOwner(),"NOT ENOUGHT MINERALS");
			return false;
		}
		System.out.println("MINERALS: "+minerals+" Price: "+price );
		return true;
	}

	
	
	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	@Override
	public void initializeName() {
		name="Action Build Starcraft";
		
	}

	@Override
	public void initializeId() {
		id=ReferenceAction.ACTION_CREATE_STARCRAFT_OBJECT;
		
	}

	@Override
	public void modifyChild(ActionInterface action) {
		if (action instanceof ActionCreateStarcraftObject){
			this.objectReference=((ActionCreateStarcraftObject) action).objectReference;
			this.price=((ActionCreateStarcraftObject) action).price;
			this.vespeneGas= ((ActionCreateStarcraftObject) action).vespeneGas;
		} 
		
	}

	public int getObjectReference() {
		return objectReference;
	}

	public void setObjectReference(int objectReference) {
		this.objectReference = objectReference;
	}

	public double getVespeneGas() {
		return vespeneGas;
	}

	public void setVespeneGas(double vespeneGas) {
		this.vespeneGas = vespeneGas;
	}

	
	
	

}
